﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InitializerService.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The initializer service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Actioners.Locations;
    using ServiceLibrary.Actioners.Profiles;
    using ServiceLibrary.Hibernate;
    using ServiceLibrary.Models;
    using ServiceLibrary.Models.Domain;

    /// <summary>
    /// The initializer service.
    /// </summary>
    [ServiceBehavior(Name = "InitializerService", InstanceContextMode = InstanceContextMode.PerCall, 
        ConcurrencyMode = ConcurrencyMode.Multiple)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class InitializerService : IInitializerService
    {
        #region Public Methods and Operators

        /// <summary>
        /// The update.
        /// </summary>
        public void Update()
        {
            NHibernateProvider.Instance.UpdateDb();

            NHibernateProvider.Instance.WithSessionAndTransaction(
                hsession =>
                    {
                        const string AdminUsername = "admin";
                        User admin =
                            hsession.QueryOver<User>().Where(u => u.Username == AdminUsername).SingleOrDefault();

                        if (admin == null)
                        {
                            admin = new User { ID = Guid.NewGuid(), Name = "Admin", Username = AdminUsername };
                            admin.EncryptedPassword = MD5Encrypter.EncryptPassword(admin.ID.ToString(), "peter");
                            hsession.Save(admin);
                        }


                        // sudo admin
                        const string sadminUsername = "s_admin";
                        User sadmin =
                            hsession.QueryOver<User>().Where(u => u.Username == sadminUsername).SingleOrDefault();

                        if (sadmin == null)
                        {
                            sadmin = new User { ID = Guid.NewGuid(), Name = "Super Admin", Username = sadminUsername, IsActive = false };
                            sadmin.EncryptedPassword = MD5Encrypter.EncryptPassword(sadmin.ID.ToString(), "sudo");
                            hsession.Save(sadmin);
                        }


                        var notActivatedUsers = hsession.QueryOver<User>().Where(x => x.IsActive == null).List();

                        foreach (var naUser in notActivatedUsers)
                        {
                            naUser.IsActive = true;
                            hsession.Save(naUser);
                        }

                        var pmActioner = new ProjectMeetingActioner(hsession, admin);
                        pmActioner.CreateDefaultProjectMeetingRelatedTypes();

                        var prActioner = new ProjectActioner(hsession, admin);
                        prActioner.CreateDefaultProjects();

                        var pActioner = new ProfileActioner(hsession, admin);
                        pActioner.CreateDefaultProfileRelatedObject();

                        var lActioner = new LocationActioner(hsession, admin);
                        lActioner.CreateDefaultLocationsRelatedObjects();

                        var cActioner = new ClusterActioner(hsession, admin);
                        cActioner.CreateClusterRelatedObjects();

                        var mpActioner = new MatchmakingPartnerActioner(hsession, admin);
                        mpActioner.CreateMatchmakingPartnerRelatedObjects();

                        var aActioner = new ActivityActioner(hsession, admin);
                        aActioner.CreateActivityRelatedObjects();

                        var niActioner = new NewsInformationActioner(hsession, admin);
                        niActioner.CreateNewsInformationRelatedObjects();
                    });
        }

        #endregion
    }
}